package com.tm.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.media.model.entity.MaterialEntity;
import com.tm.media.model.mapper.MaterialMapper;
import com.tm.media.service.MaterialService;
import com.tm.media.utils.utils.Result;
import com.tm.media.utils.utils.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

/**
 * @Author Ly
 * @Data 2023/2/23 19:11
 * @ClassName MaterialServiceImpl
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, MaterialEntity> implements MaterialService {
   @Autowired
   private MaterialMapper materialMapper;
    @Override
    public Result listMaterial(MaterialEntity materialEntity) {

        IPage<MaterialEntity> iPage = new Page<MaterialEntity>(materialEntity.getPage(),materialEntity.getSize());
        iPage.setCurrent(materialEntity.getPage());
        iPage.setSize(materialEntity.getSize());
        QueryWrapper<MaterialEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_collection",materialEntity.getIsCollection());
        List<MaterialEntity> entities = materialMapper.selectList(queryWrapper);
        return Result.SUCCESS(entities);
    }

    /**
     * 图片上传
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @Override
    public Result upload_picture(MultipartFile multipartFile) throws IOException {
        UploadFile uploadFile = new UploadFile();
        String url = uploadFile.upload_picture(multipartFile);
        return Result.SUCCESS(url);
    }

    /**
     * 收藏图片
     * @param materialEntity
     * @return
     */
    @Override
    public Result collect(MaterialEntity materialEntity) {
        materialEntity.setIsCollection(1);
        materialMapper.updateById(materialEntity);
        return Result.SUCCESS(200,"收藏成功");
    }
//删除图片
    @Override
    public Result del_picture(MaterialEntity materialEntity) {
        materialMapper.deleteById(materialEntity.getId());
        return  Result.SUCCESS(200,"删除成功");
    }
}
